######################################################################################################################################
###                                                                                                                                ###
###     Are sustainability-linked loans designed to effectively incentivize corporate sustainability? A framework for review       ###
###                                                                                                                                ###  
###                                                             SEP 2023                                                           ###
###                                                                                                                                ###
###                                                                                                                                ###
######################################################################################################################################
  


---
title: "R Notebook"
output: html_notebook
---

Load required libraries
```{r}
library(haven)
library(readxl)
library(writexl)
library(lubridate)
library(dplyr)
library(tidyr) 
library(tidyverse)
options(knitr.table.format = "html")
library(wooldridge)
library(stargazer)
library(foreign)
library(sandwich)
library(lmtest)
library(plm)
library(did)
library(DescTools)
library(data.table)
library(fixest)
library(modelsummary)
library(MatchIt)

```



Import data

```{r}
Reg_File_0_MSCI <- read_csv("Path/Reg_File_MSCI_only_treat_and_control.csv")
```

Winsorize

```{r}
Reg_File_0_MSCI <- Reg_File_0_MSCI %>% 
  mutate(wins_lev_lag = Winsorize(total_debt_total_assets,probs = c(0.01, 0.99),
          na.rm = TRUE),
         wins_btm_lag = Winsorize(`btm`,probs = c(0.01, 0.99),
          na.rm = TRUE),
         wins_size_lag = Winsorize(ln_total_asset,probs = c(0.01, 0.99),
          na.rm = TRUE),
         wins_roa_lag = Winsorize(prof,probs = c(0.01, 0.99),
          na.rm = TRUE))
```

Create Table 12 Specifications (1) - (3)

```{r}
models <- list(
  
  
reg_esg_1 <-feols(msci_score ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(industry)+as.factor(Country)
             | year_X ,cluster = c("ID"),
            data=Reg_File_0_MSCI   ),  


  
reg_esg_1 <-feols(msci_score ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(industry)*as.factor(year_X)+as.factor(Country)*as.factor(year_X)
             | ID ,cluster = c("ID"),
            data=Reg_File_0_MSCI   ),  
  
  
reg_esg_1 <-feols(msci_score ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(year_X)
             | ID ,cluster = c("ID"),
            data=Reg_File_0_MSCI   )) 


```

Create Table 12 Specifications (4) - (6)

```{r}

Reg_File_0_MSCI_e_kpi  <- subset(Reg_File_0_MSCI, issued_SLL_in_year_X_with_E_KPI == 1)

models <- list(

reg_esg_1 <-feols(e_score_msci ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(industry)+as.factor(Country)
             | year_X ,cluster = c("ID"),
            data=Reg_File_0_MSCI_e_kpi   ),  


  
reg_esg_1 <-feols(e_score_msci ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(industry)*as.factor(year_X)+as.factor(Country)*as.factor(year_X)
             | ID ,cluster = c("ID"),
            data=Reg_File_0_MSCI_e_kpi   ),  
  
  
reg_esg_1 <-feols(e_score_msci ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(year_X)
             | ID ,cluster = c("ID"),
            data=Reg_File_0_MSCI_e_kpi   ) 


)


modelsummary(dvnames(models),
             stars = c('*' = .1, '**' = .05,'***' = .01 ))
```

Create Table 12 Specifications (7) - (9)

```{r}

Reg_File_0_MSCI_s_kpi  <- subset(Reg_File_0_MSCI, issued_SLL_in_year_X_with_S_KPI == 1)

models <- list(

reg_esg_1 <-feols(s_score_msci ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(industry)+as.factor(Country)
             | year_X ,cluster = c("ID"),
            data=Reg_File_0_MSCI_s_kpi   ),  


  
reg_esg_1 <-feols(s_score_msci ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(industry)*as.factor(year_X)+as.factor(Country)*as.factor(year_X)
             | ID ,cluster = c("ID"),
            data=Reg_File_0_MSCI_s_kpi   ),  
  
  
reg_esg_1 <-feols(s_score_msci ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(year_X)
             | ID ,cluster = c("ID"),
            data=Reg_File_0_MSCI_s_kpi   ) 


)


modelsummary(dvnames(models),
             stars = c('*' = .1, '**' = .05,'***' = .01 ))
```

Create Table 12 Specifications (10) - (12)

```{r}
Reg_File_0_MSCI_ghg_kpi  <- subset(Reg_File_0_MSCI, issued_SLL_in_year_X_with_GHG_KPI == 1)

models <- list(

reg_esg_1 <-feols(climate_score_msci ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(industry)+as.factor(Country)
             | year_X ,cluster = c("ID"),
            data=Reg_File_0_MSCI_ghg_kpi   ),  


  
reg_esg_1 <-feols(climate_score_msci ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(industry)*as.factor(year_X)+as.factor(Country)*as.factor(year_X)
             | ID ,cluster = c("ID"),
            data=Reg_File_0_MSCI_ghg_kpi   ),  
  
  
reg_esg_1 <-feols(climate_score_msci ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(year_X)
             | ID ,cluster = c("ID"),
            data=Reg_File_0_MSCI_ghg_kpi   ) 


)


modelsummary(dvnames(models),
             stars = c('*' = .1, '**' = .05,'***' = .01 ))
```

Create Table 12 Specifications (13) - (15)

```{r}
Reg_File_0_MSCI_high_kpi  <- subset(Reg_File_0_MSCI, issued_SLL_in_year_X_with_high_KPI_score == 1)

models <- list(

reg_esg_1 <-feols(msci_score ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(industry)+as.factor(Country)
             | year_X ,cluster = c("ID"),
            data=Reg_File_0_MSCI_high_kpi   ),  


  
reg_esg_1 <-feols(msci_score ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(industry)*as.factor(year_X)+as.factor(Country)*as.factor(year_X)
             | ID ,cluster = c("ID"),
            data=Reg_File_0_MSCI_high_kpi   ),  
  
  
reg_esg_1 <-feols(msci_score ~   issued_SLL*Post_loan_issuance_treatment_and_control+
            as.factor(year_X)
             | ID ,cluster = c("ID"),
            data=Reg_File_0_MSCI_high_kpi   ) 


)


modelsummary(dvnames(models),
             stars = c('*' = .1, '**' = .05,'***' = .01 ))
```

Create Table 13 Whole Sample

```{r}


Reg_File_0_MSCI <- read_csv("Path/Reg_File_MSCI_only_treat_and_control_att.csv")


atts <- att_gt(yname = "msci_score", # LHS variable
               tname = "year", # time variable
               idname = "id_name", # id variable
               gname = "year_treat", # first treatment period variable
               data = Reg_File_0_MSCI, # data
               xformla = NULL # no covariates
               #xformla = ~ l_police, # with covariates
              ) # whether the data is panel or repeated cross-sectional

# Aggregate ATT
agg_effects <- aggte(atts, type = "simple")
summary(agg_effects)

# Group-time ATTs
summary(atts)

# Plot group-time ATTs
ggdid(atts)


```

Create Table 13 E KPI Sample

```{r}
Reg_File_MSCI_only_treat_and_control_att_E <- subset(Reg_File_0_MSCI, issued_SLL_in_year_X_with_E_KPI == 1)


atts <- att_gt(yname = "e_score_msci", # LHS variable
               tname = "year", # time variable
               idname = "id_name", # id variable
               gname = "year_treat", # first treatment period variable
               data = Reg_File_MSCI_only_treat_and_control_att_E, # data
               xformla = NULL # no covariates
               #xformla = ~ l_police, # with covariates
              ) # whether the data is panel or repeated cross-sectional

# Aggregate ATT
agg_effects <- aggte(atts, type = "simple")
summary(agg_effects)

# Group-time ATTs
summary(atts)

# Plot group-time ATTs
ggdid(atts)



atts <- att_gt(yname = "climate_score_msci", # LHS variable
               tname = "year", # time variable
               idname = "id_name", # id variable
               gname = "year_treat", # first treatment period variable
               data = Reg_File_MSCI_only_treat_and_control_att_E, # data
               xformla = NULL # no covariates
               #xformla = ~ l_police, # with covariates
              ) # whether the data is panel or repeated cross-sectional

# Aggregate ATT
agg_effects <- aggte(atts, type = "simple")
summary(agg_effects)

# Group-time ATTs
summary(atts)

# Plot group-time ATTs
ggdid(atts)



```

Create Table 13 S KPI Sample

```{r}
Reg_File_MSCI_only_treat_and_control_att_S <- subset(Reg_File_0_MSCI, issued_SLL_in_year_X_with_S_KPI == 1)


atts <- att_gt(yname = "s_score_msci", # LHS variable
               tname = "year", # time variable
               idname = "id_name", # id variable
               gname = "year_treat", # first treatment period variable
               data = Reg_File_MSCI_only_treat_and_control_att_S, # data
               xformla = NULL # no covariates
               #xformla = ~ l_police, # with covariates
              ) # whether the data is panel or repeated cross-sectional

# Aggregate ATT
agg_effects <- aggte(atts, type = "simple")
summary(agg_effects)

# Group-time ATTs
summary(atts)

# Plot group-time ATTs
ggdid(atts)

```

Create Table 13 C02 KPI Sample


```{r}
Reg_File_MSCI_only_treat_and_control_att_GHG <- subset(Reg_File_0_MSCI, issued_SLL_in_year_X_with_GHG_KPI == 1)



atts <- att_gt(yname = "climate_score_msci", # LHS variable
               tname = "year", # time variable
               idname = "id_name", # id variable
               gname = "year_treat", # first treatment period variable
               data = Reg_File_MSCI_only_treat_and_control_att_GHG, # data
               xformla = NULL # no covariates
               #xformla = ~ l_police, # with covariates
              ) # whether the data is panel or repeated cross-sectional

# Aggregate ATT
agg_effects <- aggte(atts, type = "simple")
summary(agg_effects)

# Group-time ATTs
summary(atts)

# Plot group-time ATTs
ggdid(atts)
```

Create Table 13 High KPI Sample

```{r}
Reg_File_MSCI_only_treat_and_control_att_high_kpi_score <- subset(Reg_File_0_MSCI, issued_SLL_in_year_X_with_high_KPI_score == 1)


atts <- att_gt(yname = "msci_score", # LHS variable
               tname = "year", # time variable
               idname = "id_name", # id variable
               gname = "year_treat", # first treatment period variable
               data = Reg_File_MSCI_only_treat_and_control_att_high_kpi_score, # data
               xformla = NULL # no covariates
               #xformla = ~ l_police, # with covariates
              ) # whether the data is panel or repeated cross-sectional

# Aggregate ATT
agg_effects <- aggte(atts, type = "simple")
summary(agg_effects)

# Group-time ATTs
summary(atts)

# Plot group-time ATTs
ggdid(atts)

```


For table 14 and 15, eventstudytools was used. Please refer to  Wolf, M. Schimmer, M., Levchenko, A., and Müller, S. (2014). EventStudyTools (Research Apps), St.Gallen. Available on: http://www.eventstudytools.com. 

Create Table 16
```{r}

load("Path/Car_analysis.Rda")



model1 <- lm(`car_-1_1` ~  number_kpis  + total_score  , data = Car_analysis)
summary(model1)

model1 <- lm(`car_-1_1` ~  number_kpis  + total_score  + prof + btm + ln_total_asset + total_debt_total_assets  , data = Car_analysis)
summary(model1)

model1 <- lm(`car_-1_1` ~  as.factor(number_kpis)  + prof + btm + ln_total_asset + total_debt_total_assets , data = Car_analysis)
summary(model1)


model1 <- lm(`car_-1_1` ~   score_dim1 + score_dim2  +score_dim3+score_dim4 +score_dim5 +score_dim6 + prof + btm + ln_total_asset + total_debt_total_assets  , data = Car_analysis)
summary(model1)
```

